Skip to content

Add support for Collection[X] | Y (for some collection types)#745

Open
jhominal wants to merge 1 commit intopython-attrs:mainfrom
jhominal:dispatch-single-collection
Open

Add support for Collection[X] | Y (for some collection types)#745
jhominal wants to merge 1 commit intopython-attrs:mainfrom
jhominal:dispatch-single-collection

Conversation

@jhominal
Copy link
Copy Markdown

fixes #743

As discussed in the issue, here is the PR.

Compared to my last comment on the issue:

I must admit, I was not too inspired for tests, there are probably more tests that could be added. The PR is also missing documentation.

@jhominal jhominal marked this pull request as ready for review April 13, 2026 20:05
@Tinche
Copy link
Copy Markdown
Member

Tinche commented Apr 14, 2026

Just letting you know this is on my radar, I'm feeling under the weather this week so it might have to wait a little

@jhominal
Copy link
Copy Markdown
Author

Thank you for commenting about your status!

Do not worry about it - there is really nothing on the line here except a possible improvement to users of cattrs.

@Tinche Tinche force-pushed the dispatch-single-collection branch from e8add33 to feb2f5b Compare April 23, 2026 14:38
Copy link
Copy Markdown
Member

@Tinche Tinche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very solid to me. Please proceed with the changelog entry and some documentation.

) -> Any:
# Design choice: only detect known concrete types as valid source types
# That avoids having to blacklist e.g. str or bytes
if isinstance(val, (deque, frozenset, list, set, tuple)):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to ask if we can test against the exact type we're expecting, but that might be too restrictive. It's very common to structure lists into tuples/frozensets etc. So I think this is perfectly fine.

# Design choice: only detect known concrete types as valid source types
# That avoids having to blacklist e.g. str or bytes
if isinstance(val, (deque, frozenset, list, set, tuple)):
return converter.structure(val, collection_type)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A trick you can do is finding these hooks in the hook factory itself (so in make_structure_union_single_collection, instead of in structure_union_single_collection) using converter.get_structure_hook, and then just using them here.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 23, 2026

Unable to generate the performance report

There was an internal error while processing the run's data. We're working on fixing the issue. Feel free to contact us on Discord or at support@codspeed.io if the issue persists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement a strategy to resolve natively Sequence[X] | Y

2 participants